<template>
  <div class="exam">
    <el-button @click="tocheck" type="primary" icon="el-icon-s-custom"
      >登录系统</el-button
    >
  </div>
</template>
<script>
import cookie from 'js-cookie'
export default {
  name: 'AppsName',
  data() {
    return {
      val: '',
    }
  },
  methods: {
    tocheck() {
      var userid = cookie.get('phone')
      this.checkuser(userid, 1) //1-学生，2-老师
    },
    peixun(a, b) {
      this.loadScript('//js.andisk.com/appjs/metadata.js', () => {
        var e = {}
        var c = new Date().getTime()
        if (typeof b == 'undefined' || b == null) {
          b = 1
        }
        e.userid = a
        e.type = b
        e.intime = c
        e.sign = hex_md5('gkhx' + c)
        // var d = 'https://admin.andisk.com/andisk/servlet/topeixun'
        var d = 'https://peixun.met-ec.com/andisk/servlet/topeixun'
        $.ajax({
          type: 'POST',
          url: d,
          data: e,
          async: false,
          dataType: 'jsonp',
          success: (i) => {
            console.log('')
            var h = i
            if (h.rtn == 1) {
              var j = h.app
              var f = h.user
              var g = ''
              if (f.role >= 300) {
                g =
                  j.web_url +
                  '/' +
                  j.template +
                  '/console.html?ud=' +
                  meta.compile(a)
                if (j.web_url.indexOf('andisk.com') < 0) {
                  g = j.web_url + '/console.html?ud=' + meta.compile(a)
                }
              } else {
                if (f.role >= 200) {
                  g =
                    j.web_url +
                    '/' +
                    j.template +
                    '/ta.html?sid=' +
                    meta.compile(a)
                  if (j.web_url.indexOf('andisk.com') < 0) {
                    g = j.web_url + '/ta.html?sid=' + meta.compile(a)
                  }
                } else {
                  g =
                    j.web_url +
                    '/' +
                    j.template +
                    '/p.html?sid=' +
                    meta.compile(a)
                  if (j.web_url.indexOf('andisk.com') < 0) {
                    g = j.web_url + '/p.html?sid=' + meta.compile(a)
                  }
                }
              }
              this.to(g)
            } else {
              if (h.rtn == -1) {
                var j = h.app
                if (typeof j == 'undefined') {
                  // alert("用户信息有错误，请确认后再试");
                  this.$message.error('用户信息有错误，请确认后再试')
                  return
                }
                var g = j.web_url + '/' + j.template + '/bind.html?ud=' + a
                if (j.web_url.indexOf('andisk.com') < 0) {
                  g = j.web_url + '/bind.html?ud=' + a
                }
                this.to(g)
              } else {
                if (h.rtn == 0) {
                  this.$message.error('培训考试系统异常')
                  // var g = "//js.andisk.com/andisk/app/error.html?c=-1";
                  // to(g)
                }
              }
            }
          },
          error: function (f, h, g) {
            console.log('')
          },
        })
      })
    },
    checkuser(a, b) {
      this.loadScript('//js.andisk.com/appjs/metadata.js', () => {
        let type = ''
        var e = {}
        var c = new Date().getTime()
        e.userid = a
        e.intime = c
        e.sign = hex_md5('gkhx' + c)
        var d = 'https://peixun.met-ec.com/andisk/servlet/checkuser'
        $.ajax({
          type: 'POST',
          url: d,
          data: e,
          async: false,
          dataType: 'jsonp',
          success: (g) => {
            if (g.rtn === 1) {
              this.peixun(a, b)
            } else {
              this.$message.success('您还没有学生使用权限，请先联系您的老师！')
            }
          },
          error: (f, h, g) => {
            console.log('f', f, 'h', 'g', g)
          },
        })
      })
    },
    to(a) {
      var b = document.createElement('a')
      b.target = '_blank'
      b.style.display = 'none'
      b.href = a
      document.body.appendChild(b)
      b.click()
      return
    },
    loadScript(a, c) {
      if ($("script[src='" + a + "']").length > 0) {
        if (c) {
          c(2)
        }
        return
      }
      var d = document.getElementsByTagName('head')[0]
      var b = document.createElement('script')
      b.setAttribute('type', 'text/javascript')
      b.setAttribute('src', a)
      d.appendChild(b)
      b.onload = b.onreadystatechange = function () {
        if (
          !this.readyState ||
          this.readyState == 'loaded' ||
          this.readyState == 'complete'
        ) {
          if (c) {
            c(1)
          }
          return
        }
      }
    },
  },
}
</script>
<style lang="less" scoped>
.exam {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
